package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.domain.*;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.exception.UserException;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.interceptor.header.HeaderService;
import com.mk.cloud.common.interceptor.header.IHLogin;
import com.mk.cloud.common.util.DateUtils;
import com.mk.cloud.common.util.ResUtils;
import com.mk.cloud.pt.service.app.RewardService;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

@Controller("order")
public class POrderController extends CookieService {

    @Inject
    private POrder.Mapper pOrderMapper;

    @Description("我的订单列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<POrder> pOrderList(
            String orderid,
            String phone,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<POrder> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        map.put("coid", me.getCoid());
        map.put("orderid", orderid);
        map.put("phone", phone);
        query.setParas(map);
        pOrderMapper.getSQLManager().pageQuery("pOrder.select_order_page_list", POrder.class, query);
        return query;
    }


    @Description("标记订单状态")
    @With(ICLogin.class)
    @PutAction("changeOrderState.html")
    public int changeOrderState(
            @Description("订单编号") String orderid

    ) {
        POrder po = new POrder();
        po.setOrderid(orderid);
        po = pOrderMapper.templateOne(po);
        if(GlobalValues.ORDER_STATE_2.value().equals(po.getOrderstate())) {
            po.setOrderstate("3");
            po.setFinishdate(DateUtils.getTimeNow());
            return pOrderMapper.updateTemplateById(po);
        } else  {
            throw new UserException("只有已付款的订单可以标记为线下已处理！");
        }
    }

    @Description("获取订单")
    @With(ICLogin.class)
    @GetAction("getOrderById.json")
    public POrder getOrderById(
            @Description("订单编号") String orderid

    ) {
        Map<String, Object> param = new HashMap<>();
        param.put("orderid", orderid);
        return pOrderMapper.getSQLManager().selectSingle("pOrder.orderDetail", param,POrder.class);
    }
}
